#!/usr/bin/env python3

import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import res.qrc_ui
from session import Session
uiName = 'view/note.ui'

class MyDelegate(QItemDelegate):
    def __init__(self, parent=None, *args):
        QItemDelegate.__init__(self, parent, *args)

    def paint(self, painter, option, index):
        painter.save()

        # set background color
        painter.setPen(QPen(Qt.NoPen))
        if option.state & QStyle.State_Selected:
            linearGrad = QLinearGradient(QPointF(0, 0), QPointF(200, 200));  
            linearGrad.setColorAt(0, Qt.darkGreen);  
            linearGrad.setColorAt(1, Qt.yellow);  
            painter.setBrush(QBrush(linearGrad))
        else:
            painter.setBrush(QBrush(Qt.gray))
        painter.drawRect(option.rect)

        # set text color
        painter.setPen(QPen(Qt.black))
        value = index.data(Qt.DisplayRole)
        #if value.isValid():
        #    text = value.toString()
        painter.drawText(option.rect, Qt.AlignLeft, value)

        painter.restore()

class NoteWindow(QtGui.QMainWindow):
    
    def __init__(self, parent=None):
        #QtGui.QWidget.__init__(self, parent)
        super(NoteWindow, self).__init__(parent)
        uic.loadUi(uiName, self)
        self.center()
        #print(Session().getAttribute('user'))

        self.model = QtGui.QStandardItemModel(self.noteListView)
        item = QtGui.QStandardItem()
        item.setText('\nItem\ntext\n')
        
    
        #item.setIcon("")
        textual_item = QtGui.QStandardItem('Item text2')
        self.model.appendRow(item)
        self.model.appendRow(textual_item)
        self.noteListView.setModel(self.model)
        self.noteListView.setItemDelegate(MyDelegate(self))

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    view = NoteWindow()
    view.show()
    sys.exit(app.exec_())